package com.fenrirtec.aepp.manage.api;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.TenderBidlistDto;
import com.fenrirtec.aepp.common.service.TenderBidService;
import com.fenrirtec.aepp.common.service.impl.TenderBidServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class TenderBidSuppliersAPI extends JSONActionSupport{
	
	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderBidSuppliersAPI.class);

	public TenderBidSuppliersAPI() {
	}


	private String param;

	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	@Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}
	
	@Override
	protected JSONObject doExecute() {
		TenderBidService tenderBidservice = new TenderBidServiceImpl();
		JSONObject root = new JSONObject();
		List<TenderBidlistDto> tenderBidDtolist = new ArrayList<TenderBidlistDto>();
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);	
			Integer tenderid=jsonObj.getInteger("tenderId");			
			tenderBidDtolist=tenderBidservice.findTenderBidSuppliers(tenderid);
						
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} 
		
		if(tenderBidDtolist !=null && tenderBidDtolist.size()>0){
			for(int i=0;i<tenderBidDtolist.size();i++){
				if(tenderBidDtolist.get(i).getPath() != null){
					//tenderBidDtolist.get(i).setPath("<input class='easyui-linkbutton' type='button' value='下载' style='height:19px;'>");
					tenderBidDtolist.get(i).setPath("资质文件");
				}
			}
			root.put("total", tenderBidDtolist.size());
			root.put("tbslist", JsonUtils.fromBean(tenderBidDtolist));
		}
		return root;
	}	
}
